/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.dreamlu.system.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import net.dreamlu.common.annotation.SysLog;
import net.dreamlu.common.base.BaseController;
import net.dreamlu.common.result.EasyPage;
import net.dreamlu.common.result.PageVO;
import net.dreamlu.mica.core.result.R;
import net.dreamlu.system.model.Item;
import net.dreamlu.system.model.Order;
import net.dreamlu.system.service.IItemService;
import net.dreamlu.system.service.IOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author L.cm
 * @since 2018-01-29
 */
@Controller
@RequestMapping("/order")
@AllArgsConstructor
public class orderController extends BaseController {
	private IOrderService orderService;


	/*@GetMapping("/manager")*/
	@RequestMapping("/manager")
	public String manager() {
		return "system/order/orderList";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public EasyPage<Order> dataGrid(Order order, PageVO pageVO) {
		QueryWrapper<Order> ew = new QueryWrapper<>(order);
		Page<Order> pages = pageVO.toPage();
		orderService.page(pages, ew);
		return EasyPage.of(pages);
	}

	/**
	 * 添加页面
	 */
	@RequestMapping("/addPage")
	public String addPage() {
		return "system/order/orderAdd";
	}

	/**
	 * 添加
	 */
	@PostMapping("/add")
	@PreAuthorize("@sec.hasPermission('order:add')")
	@ResponseBody
	public R<Object> add(@Valid Order order) {
		return status(orderService.save(order));
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/editPage")
	public String editPage(Model model, Integer orderId) {
		Order order = orderService.getById(orderId);
		model.addAttribute("order", order);
		return "system/order/orderEdit";
	}

	@PostMapping("/edit")
	@PreAuthorize("@sec.hasPermission('order:edit')")
	@ResponseBody
	public R<Object> edit(@Valid Order order) {
		return status(orderService.updateById(order));
	}

	/**
	 * 删除1212
	 */
	@PostMapping("/delete")
	@PreAuthorize("@sec.hasPermission('order:delete')")
	@ResponseBody
	@SysLog("删除商品")
	public R<Object> delete(Order order) {
		return status(orderService.removeById(order));
	}
}
